package com.tj.mybatisplusdemo.domain.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tj.mybatisplusdemo.application.UserApplication;
import com.tj.mybatisplusdemo.domain.user.entity.UserBaseInfo;
import com.tj.mybatisplusdemo.infrastructure.persistent.mapper.user.UserBaseInfoMapper;
import com.tj.mybatisplusdemo.interfaces.dto.UserNameDto;
import com.tj.mybatisplusdemo.interfaces.vo.UserInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName（项目名称）:
 * @package（包）: com.tj.mybatisplusdemo.domain.user.service
 * @description（类描述）:
 * @author（创建人）: tangjing
 * @createDate（创建时间）: 2022/05/24 17:09
 * @updateUser（修改人）:
 * @updateDate（修改时间）:
 * @updateRemark（修改备注）:
 * @version（版本）: v1.0
 */
@Service
public class UserService extends ServiceImpl<UserBaseInfoMapper, UserBaseInfo> implements UserApplication {

    @Resource
    private UserBaseInfoMapper mapper;

    @Override
    public List<UserBaseInfo> listAllUser() {
        // 使用构造器查询所有用户和用户数量
        LambdaQueryWrapper<UserBaseInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserBaseInfo::getDelStatus,0);
        List<UserBaseInfo> list = mapper.selectList(wrapper);
        list.forEach(System.out::println);
        Integer count = mapper.selectCount(wrapper);
        System.out.println("一共有"+count+"位用户");
        return list;
    }

    @Override
    public List<UserInfoVO> listUserByName(UserNameDto dto) {
        LambdaQueryWrapper<UserBaseInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(UserBaseInfo::getUserName,dto.getUserName());
        List<UserBaseInfo> list = mapper.selectList(wrapper);
        List<UserInfoVO> result = new ArrayList<>(16);
        UserInfoVO item ;
        for (UserBaseInfo info:list) {
            item=new UserInfoVO();
            BeanUtils.copyProperties(info,item);
            result.add(item);
        }
        return result;
    }
}
